﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PrimeFactorization
{
    class FactorsOfANumber
    {

       
        public string[] factors_of_number(bool ComingFromACForm,int number, Nullable<int> BXNumber)
        {
            string[] FactorsToReturn = new string[1000];
            
            int[] factortracker = new int[1000];
            
            int inc = 0;
            int inc2;
            int test = 0;
            int numforreturnfactorarray = 0;
            int factor = 0;
            int result = 0;
            int divisionnumforifnegativeorpos=0;
            bool SpecialCase = false;


            while (factor != number)
            {
                if (number < 0)
                { factor--; }
                if (number > 0)
                { factor++; }

                result = number % factor;
                if (result == 0)
                {
                    
                    result = number / factor; //Result is really one factor and factor is the other

                    if (BXNumber != null)
                    {
                        if (result + factor == BXNumber)
                        { test = 0; }
                        else if ((result + factor) * -1 == BXNumber && factor >0)
                        { test = 0;
                        SpecialCase = true;
                            
                     
                        }
                        else if (result + factor != BXNumber)
                        { test = 2; }
                     


                    }
                    factortracker[inc] = result + factor;
                    inc2 = inc;
                    inc++; //Bug was here

                    while ((inc2 > 0 & ComingFromACForm!=true)||(inc2>0 & number>0)) //Make sure the new factor is not already listed. EI 3*5 and 5*3 //Skipped for AC Method Calling
                    {
                        inc2--;

                        if (factortracker[inc2] == result + factor)
                        {

                            test = 2;
                            break;
                        }
                        if (BXNumber == null && factortracker[inc2] == (result + factor) * -1)
                        {
                            test = 2;
                            break;
                        }

                    }
                 
                    if (test != 2)
                    {
                        string resultdsiplay = "Error";
                        if(BXNumber==null){resultdsiplay = string.Format("{0:00}  *  {1:00}", factor, result);}
                        
                        else{
                            if (factor + result == BXNumber && SpecialCase ==false)
                            {
                                resultdsiplay = string.Format("{0:00}  +  {1:00}", factor, result);
                            }
                            if ((factor * -1)+(result * -1) == BXNumber && SpecialCase==true)
                            {
                                resultdsiplay = string.Format("{0:00}  +  {1:00}", factor*-1, result*-1);
                                SpecialCase = false;
                            }
                        }

                        
                        FactorsToReturn[numforreturnfactorarray] = resultdsiplay;
                            numforreturnfactorarray++;
                       
                    }

                }


            }

            return FactorsToReturn;
        }
    }
}
